<?php

class VoucherController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $req = $this->getRequest();
	   	$auth = new Application_Model_Auth();
		$rurl = '/Voucher';
		 if (!$auth->isLoggedIn()){
			 echo '<script>alert("请先登录")</script>';
				$this->_redirect('/login?rurl='.$rurl);
		}else{
			$rest = new Application_Model_Rest();
			$voucherList = $rest->getVoucherList();
			$this->view->VoucherList=$voucherList;
			if($req->isPost()){
				$code = $req->getPost('wf_name');
				$voucher = new Application_Model_VoucherMapper();
				$result = $voucher->getVoucher($code);
				header("Content-Type: text/html; charset=utf-8");
				$time = date("y-m-d h:i:s",time());
				if(empty($result)){
						echo '<script>alert("兑换码错误")</script>';
				}else if($result[0]['user_id']>0||$result[0]['user_id']>0){
						echo '<script>alert("领取失败，现金券已被领取")</script>';
				}else if($result[0]['deadline']<$time){
						echo '<script>alert("现金券已过期")</script>';
					}else{
						//兑换
						$auth = new Application_Model_Auth();
						$userid = $auth ->getUserId();
						$res = $voucher->updateUser($result[0]['id'],$userid);
						if($res){
							echo '<script>alert("兑换成功")</script>';
							echo "<script> window.location.href=window.location.href;</script>";
						}
					}
				}
			}
    }


}

